package com.xiangban.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiangban.exception.BizException;
import com.xiangban.model.TSystem;
import com.xiangban.model.TUserBase;
import com.xiangban.model.dto.LoginDto;
import com.xiangban.model.dto.RegisterDto;
import com.xiangban.model.dto.WeChatUserInfoDto;
import com.xiangban.model.res.ApiResult;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.service.UserBaseService;
import com.xiangban.utils.CommonUtil;
import com.xiangban.utils.HttpClientUtil;
import com.xiangban.utils.WeChatUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RefreshScope
@RequestMapping("/user")
public class UserBaseController {
    @Autowired
    private UserBaseService userBaseService;
    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private HttpServletRequest request;

    /**
     * 用户信息注册
     *
     * @param registerDto 用户基本信息
     */
    @RequestMapping("/register")
    public ApiResult<Object> register(@RequestBody RegisterDto registerDto) {
        userBaseService.register(registerDto);
        return ApiResult.newSuccess();

    }

    /**
     * 用户登录
     *
     * @param loginDto
     * @return
     */
    @RequestMapping("/login")
    public ApiResult<Object> login(@RequestBody LoginDto loginDto) {
        return ApiResult.newSuccess(userBaseService.login(loginDto));

    }


    /**
     * 获取微信登录信息 1
     *
     * @param weChatUserInfoDto
     * @return sessionKey
     */
    @RequestMapping("/getSessionKey")
    public ApiResult<Object> getSessionKey(@RequestBody WeChatUserInfoDto weChatUserInfoDto) {
        HashMap<String, Object> res = userBaseService.getSessionKey(weChatUserInfoDto);
        return ApiResult.newSuccess(res);
    }

    /**
     * 获取用户信息
     *
     * @return 用户基本信息
     */
    @RequestMapping("/getUserInfo")
    public ApiResult<Object> getUserInfo(@RequestParam String token) {
        return userBaseService.getUserInfo(token);

    }


    @PostMapping("/resetPassword")
    @ApiOperation("忘记密码")
    public ApiResult<Object> resetPassword(@RequestBody LoginDto loginDto) {
        userBaseService.resetPassword(loginDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("修改用户信息")
    public ApiResult<Object> updateUserInfo(@RequestBody RegisterDto registerDto) {
        userBaseService.updateUserInfo(registerDto);
        return ApiResult.newSuccess();
    }

    @RequestMapping("/getUserList")
    @ApiOperation("获取所有用户信息")
    public ApiResult<Object> getUserList() {
        return ApiResult.newSuccess(userBaseService.getUserList());
    }

    @PostMapping("/getAccessToken")
    @ApiOperation("获取AccessToken")
    public ApiResult<Object> getAccessToken(@RequestBody TSystem tSystem) {//access_id
        return ApiResult.newSuccess(userBaseService.getAccessToken(tSystem));
    }

    @RequestMapping("/getSignature")
    public ApiResult<Object>  getSignature(@RequestBody Map<String ,String> map) {
        TSystem tSystem = new TSystem();
        tSystem.setType(Integer.parseInt(map.get("type")));
        tSystem.setAccessId(map.get("accessId"));
        return ApiResult.newSuccess(userBaseService.getSignature(tSystem,map.get("url")));

    }

}
